package com.mingmen.canting.defineview;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.mingmen.canting.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/15.
 * 自定义支付密码组件
 */

public class PayPassView extends RelativeLayout {
    private Activity mContext;//上下文
    private GridView mGridView; //支付键盘
    private String strPass = "";//保存密码

    private ImageView mImageViewClose;//关闭

    private TextView mTvHint;//提示 (提示:密码错误,重新输入)
    private List<Integer> listNumber;//1,2,3---0
    private View mPassLayout;//布局
    private boolean isRandom;

    ClearEditText edit_pwd;

    Button btn_cancel;

    /**
     * 按钮对外接口
     */
    public static interface OnPayClickListener {
        void onPassFinish(String password);

        void onPayClose();

        void onPayForget();
    }

    private OnPayClickListener mPayClickListener;

    public void setPayClickListener(OnPayClickListener listener) {
        mPayClickListener = listener;
    }

    public PayPassView(Context context) {
        super(context);
    }

    //在布局文件中使用的时候调用,多个样式文件
    public PayPassView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //在布局文件中使用的时候调用
    public PayPassView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = (Activity) context;

        initView();//初始化
        this.addView(mPassLayout); //将子布局添加到父容器,才显示控件
    }

    /**
     * 初始化
     */
    private void initView() {
        mPassLayout = LayoutInflater.from(mContext).inflate(R.layout.view_paypass_layout, null);

        mImageViewClose = (ImageView) mPassLayout.findViewById(R.id.iv_close);//关闭

        mTvHint = (TextView) mPassLayout.findViewById(R.id.tv_passText);//提示文字

        mGridView = (GridView) mPassLayout.findViewById(R.id.gv_pass);
        btn_cancel = mPassLayout.findViewById(R.id.btn_cancel);
        edit_pwd = mPassLayout.findViewById(R.id.edit_pwd);
        mImageViewClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanAllTv();
                mPayClickListener.onPayClose();
            }
        });
        btn_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanAllTv();
                mPayClickListener.onPayClose();
            }
        });
        //初始化数据
        initData();
    }

    /**
     * isRandom是否开启随机数
     */
    private void initData() {
        listNumber = new ArrayList<>();
        listNumber.clear();
        for (int i = 1; i <= 9; i++) {
            listNumber.add(i);
        }
        listNumber.add(10);
        listNumber.add(0);
        listNumber.add(R.mipmap.ic_pay_del0);
//            listNumber.add(12);
        mGridView.setAdapter(adapter);
    }


    /**
     * GridView的适配器
     */

    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return listNumber.size();
        }

        @Override
        public Object getItem(int position) {
            return listNumber.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.view_paypass_gridview_item, null);
                holder = new ViewHolder();
                holder.btnNumber = (TextView) convertView.findViewById(R.id.btNumber);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            //-------------设置数据----------------
            holder.btnNumber.setText(listNumber.get(position) + "");
            //监听事件----------------------------
            if (position == 11) {
                holder.btnNumber.setText("完成");
            }
            if (position == 9) {//空按钮
                holder.btnNumber.setText(".");
            }
            holder.btnNumber.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position < 11 && position != 9) {//0-9按钮
                        if (strPass.length() > 2) {
                            return;
                        } else {
                            strPass = strPass + listNumber.get(position);
                            edit_pwd.setText(strPass);
                        }

                        Log.d("strPasslength", strPass.length() + "");
                        //输入完成
                    } else if (position == 11) {//删除
                        if (strPass.equals("0")) {
                            mPayClickListener.onPassFinish(strPass);//请求服务器验证密码
                            cleanAllTv();
                            mPayClickListener.onPayClose();
                        } else if (strPass.startsWith("0")) {
                            Toast.makeText(mContext, "输入的数字不符合要求", Toast.LENGTH_SHORT).show();
                            cleanAllTv();
                        } else {
                            mPayClickListener.onPassFinish(strPass);//请求服务器验证密码
                            cleanAllTv();
                            mPayClickListener.onPayClose();
                        }
                    }
                }
            });

            return convertView;
        }
    };

    static class ViewHolder {
        public TextView btnNumber;
    }

    /***
     * 设置随机数
     * @param israndom
     */
    public PayPassView setRandomNumber(boolean israndom) {
        isRandom = israndom;
        initData();
        adapter.notifyDataSetChanged();
        return this;
    }

    /**
     * 关闭图片
     * 资源方式
     */
    public PayPassView setCloseImgView(int resId) {
        mImageViewClose.setImageResource(resId);
        return this;
    }

    /**
     * 关闭图片
     * Bitmap方式
     */
    public PayPassView setCloseImgView(Bitmap bitmap) {
        mImageViewClose.setImageBitmap(bitmap);
        return this;
    }

    /**
     * 关闭图片
     * drawable方式
     */
    public PayPassView setCloseImgView(Drawable drawable) {
        mImageViewClose.setImageDrawable(drawable);
        return this;
    }


    /**
     * 设置忘记密码文字
     */
    public PayPassView setForgetText(String text) {

        return this;
    }

    /**
     * 设置忘记密码文字大小
     */
    public PayPassView setForgetSize(float textSize) {

        return this;
    }

    /**
     * 设置忘记密码文字颜色
     */
    public PayPassView setForgetColor(int textColor) {

        return this;
    }

    /**
     * 设置提醒的文字
     */
    public PayPassView setHintText(String text) {
        mTvHint.setText(text);
        return this;
    }

    /**
     * 设置提醒的文字大小
     */
    public PayPassView setTvHintSize(float textSize) {
        mTvHint.setTextSize(textSize);
        return this;
    }

    /**
     * 设置提醒的文字颜色
     */
    public PayPassView setTvHintColor(int textColor) {
        mTvHint.setTextColor(textColor);
        return this;
    }

    /**
     * 清楚所有密码TextView
     */
    public PayPassView cleanAllTv() {
        strPass = "";
        for (int i = 0; i < 6; i++) {
//            mTvPass[i].setText("");
            edit_pwd.setText("");
            edit_pwd.setHint("请输入所需数量..");
        }
        return this;
    }
}
